
#include "tos_secure_init.h"
#include "tos_secure_context.h"
#include "tos_secure_port_macros.h"
/**
 * @brief Constants required to manipulate the SCB.
 */
#define SCB_AIRCR                 ( ( volatile uint32_t * ) 0xe000ed0c )  /* Application Interrupt and Reset Control Register. */
#define SCB_AIRCR_VECTKEY_POS     ( 16UL )
#define SCB_AIRCR_VECTKEY_MASK    ( 0xFFFFUL << SCB_AIRCR_VECTKEY_POS )
#define SCB_AIRCR_PRIS_POS        ( 14UL )
#define SCB_AIRCR_PRIS_MASK       ( 1UL << SCB_AIRCR_PRIS_POS )

void tos_secure_deprioritize_nsexceptions( void ){
    uint32_t ulIPSR;

    /* Read the Interrupt Program Status Register (IPSR) value. */
    secure_get_IPSR(ulIPSR);

    /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
     * when the processor is running in the Thread Mode. */
    if( ulIPSR != 0 )
    {
        *( SCB_AIRCR ) = ( *( SCB_AIRCR ) & ~( SCB_AIRCR_VECTKEY_MASK | SCB_AIRCR_PRIS_MASK ) ) |
                                ( ( 0x05FAUL << SCB_AIRCR_VECTKEY_POS ) & SCB_AIRCR_VECTKEY_MASK ) |
                                ( ( 0x1UL << SCB_AIRCR_PRIS_POS ) & SCB_AIRCR_PRIS_MASK );
    }
}